GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

treeToFlatList.js ➔ ???   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 7
c 1
b 1
f 0
nc 12
dl 0
loc 60
rs 7.4661
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import { List, Map, fromJS } from 'immutable';
2
3
export const treeToFlatList = (
4
    data,
5
    rootIdentifier = 'root',
6
    childIdentifier = 'children'
7
) => {
8
9
    if (!data) {
10
        throw new Error('Expected data to be defined');
11
    }
12
13
    const result = [];
14
    const cfg = { flatIndex: 0 };
15
    let stack = List();
16
17
    if (!Map.isMap(data)) {
18
        data = fromJS(data);
19
    }
20
21
    if (data.get(rootIdentifier)) {
22
        data = data.get(rootIdentifier);
23
24
        stack = stack.push(
25
            toItem(List(), childIdentifier, cfg)(data)
26
        );
27
    }
28
    else {
29
        stack = data.get(childIdentifier).map(
30
            toItem(List([-1]), List([0]), childIdentifier)
31
        );
32
    }
33
34
    while (stack.count()) {
35
36
        const item = stack.first();
37
        const children = item.get(childIdentifier);
38
39
        stack = stack.shift();
40
41
        if (List.isList(children) && !item.get('_hideChildren')) {
42
            stack = children.map(
43
                toItem(
44
                    item.get('_path').push(item.get('_id')),
45
                    childIdentifier,
46
                    cfg,
47
                    item,
48
                    children
49
                )
50
            ).concat(stack);
51
        }
52
53
        // removing erroneous data since grid uses internal values
54
        result.push(
55
            item.delete(childIdentifier)
56
                .delete('parentId')
57
                .delete('id')
58
        );
59
    }
60
61
    return List(result);
62
};
63
64
const toItem = (
0 ignored issues
show
Unused Code introduced by
The constant toItem seems to be never used. Consider removing it.
Loading history...
65
    path, childIdentifier, cfg, parent, siblings = List()
66
) => (node, index = 0) => {
67
68
    const previousSibling = index - 1 > -1
69
        ? siblings.get(index - 1)
70
        : undefined;
71
72
    const previousSiblingTotalChilden = (
73
        previousSibling && previousSibling.get(childIdentifier)
74
            ? previousSibling.get(childIdentifier).count()
75
            : 0
76
    );
77
78
    return node.merge({
79
        _id: node.get('id'),
80
        _parentId: node.get('parentId', 'root'),
81
        _parentIndex: parent ? parent.get('_index') : 0,
82
        _depth: path.count(),
83
        _hideChildren: node.get('_hideChildren'),
84
        _hasChildren: (
85
            node.get(childIdentifier) && node.get(childIdentifier).count() > 0
86
        ),
87
        _index: index,
88
        _flatIndex: cfg.flatIndex++,
89
        _isFirstChild: index === 0,
90
        _isLastChild: index === siblings.count() - 1,
91
        _previousSiblingId: previousSibling
92
            ? previousSibling.get('id')
93
            : undefined,
94
        _previousSiblingTotalChilden: previousSiblingTotalChilden,
95
        _key: `tree-item-${node.get('id')}`,
96
        _isExpanded: (
97
            node.get(childIdentifier)
98
                && node.get(childIdentifier).count() > 0
99
                && !node.get('_hideChildren')
100
        ),
101
        _leaf: !(
102
            (node.get(childIdentifier) && node.get(childIdentifier).count() > 0)
103
            || (node.get('leaf') !== undefined && node.get('leaf') === false)
104
        ),
105
        _path: path
106
    });
107
108
};
109